/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import POJO.KhuVuc;
import UTIL.HibernateUtil;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author ChanhTin
 */
public class KhuVucDAO {
     
    /**
     * @desc lay danh sach khu vuc
     * @return 
     */
    public static List<KhuVuc> layDanhSach() {
        List<KhuVuc> ds = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            String hql = "select kv from KhuVuc kv";
            org.hibernate.Query query = session.createQuery(hql);
            ds = query.list();
        } catch (HibernateException ex) {
            System.err.println(ex);
        } finally {
            session.close();
        }
        return ds;  
    }
    
    /**
     * @desc tim khu vuc theo ma khu vuc
     * @param maKhuVuc
     * @return 
     */
    public static KhuVuc layThongTinKhuVuc(int maKhuVuc) {
        KhuVuc kv = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            kv = (KhuVuc) session.get(KhuVuc.class, maKhuVuc);
        } catch (HibernateException ex) {
            System.err.println(ex);
        } finally {
            session.close();
        }
        return kv;
    }
    
    /**
     * @desc them khu vuc
     * @param kv
     * @return 
     */
    public static boolean themKhuVuc(KhuVuc kv) throws HibernateException{
        Session session = HibernateUtil.getSessionFactory().openSession(); 
        if (KhuVucDAO.layThongTinKhuVuc(kv.getMaKhuVuc())!=null) { 
            return false; 
        }
        boolean flag = true;
        Transaction transaction = null; 
        try { 
            transaction = session.beginTransaction(); 
            session.save(kv); 
            transaction.commit(); 
        } catch (HibernateException ex) { 
            //Log the exception 
            transaction.rollback(); 
            //System.err.println(ex);
            flag = false;
        } finally { 
            session.close(); 
        } 
        return flag;
    }
    
    /**
     * @desc cap nhat thong tin khu vuc
     * @param kv
     * @return 
     */
    public static boolean capNhatThongTinKhuVuc(KhuVuc kv) throws HibernateException{ 
        Session session = HibernateUtil.getSessionFactory().openSession(); 
        if (KhuVucDAO.layThongTinKhuVuc(kv.getMaKhuVuc()) == null) { 
            return false; 
        }
        boolean flag = true;
        Transaction transaction = null; 
        try { 
            transaction = session.beginTransaction(); 
            session.update(kv); 
            transaction.commit(); 
        } catch (HibernateException ex) { 
            //Log the exception 
            transaction.rollback(); 
           //System.err.println(ex);
            flag = false;
        } finally { 
            session.close(); 
        } 
        return flag; 
    } 
    
    /**
     * @desc xoa khu vuc
     * @param maKhuVuc
     * @return 
     */
    public static boolean xoaKhuVuc(int maKhuVuc) throws HibernateException{ 
        Session session = HibernateUtil.getSessionFactory().openSession(); 
        KhuVuc kv = KhuVucDAO.layThongTinKhuVuc(maKhuVuc); 
        if(kv==null){ 
            return false; 
        }
        boolean flag = true;
        Transaction transaction = null; 
        try { 
            transaction = session.beginTransaction(); 
            session.delete(kv); 
            transaction.commit(); 
        } catch (HibernateException ex) { 
            //Log the exception 
            transaction.rollback(); 
            //System.err.println(ex);
            flag = false;
        } finally { 
            session.close(); 
        } 
        return flag; 
    }
}
